﻿package kit4j.tests.db;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.springframework.util.FileCopyUtils;

public class TestProcedure {

	/**
	 * @param args
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 * @throws IOException 
	 */
	public static void main(String[] args) throws Exception {
		System.out.println("------Start test...");
		
		testFileBytes();
		
		System.out.println("------Test end...");
	}
	
	static void testFileBytes() throws FileNotFoundException, IOException {
		String fileName = "F:/1.txt";
		File file = new File(fileName);
		
		System.out.println(file.length());
		System.out.println();
		
		PrintWriter out = new PrintWriter("F:/1.1.txt");
		out.print(new String(FileCopyUtils.copyToByteArray(file)));
		out.close();
		
		System.out.println(new String(copyToByteArray(new FileInputStream(file), (int) file.length())).length());
	}
	
	static byte[] copyToByteArray(InputStream in, int length) throws IOException {
		byte[] bytes = new byte[length];
		
		in.read(bytes);
		
		return bytes;
	}

	static void testMysql() throws ClassNotFoundException, SQLException, IOException {
		Class.forName("com.mysql.jdbc.Driver");

		Connection conn = DriverManager.getConnection(
				"jdbc:mysql://localhost:3306/test", "root", "www.wapdm.com");
		
		try {
			insertFile(conn);
			selectFile(conn);
		} finally {
			conn.close();
		}
	}
	
	static void insertFile(Connection conn) throws SQLException, IOException {
		String sql = "insert into T_FILE (file_name, bytes) values (?, ?)";
		PreparedStatement ptmt = conn.prepareStatement(sql);

		try {
			String fileName = "F:/1.txt";
			
			FileInputStream input = new FileInputStream(fileName);
			
			try {
				ptmt.setString(1, fileName);
				ptmt.setBlob(2, input, (int) input.getChannel().size());
				
				if(ptmt.executeUpdate() == 1) {
					System.out.println("yes");
				} else {
					System.out.println("no");
				}
			} finally {
				input.close();
			}
		} finally {
			ptmt.close();
		}
	}
	
	static void selectFile(Connection conn) throws SQLException {
		String selectSql = "select * from T_FILE";
		Statement stmt = conn.createStatement();
		ResultSet selectRs = stmt.executeQuery(selectSql);
		if(selectRs.first()) {
			do {
				System.out.println("id:" + selectRs.getString("id") + ",fileName:" + selectRs.getString("file_name"));
			} while(selectRs.next());
		}
	}

	static void testOracle() throws ClassNotFoundException, SQLException {
		Class.forName("oracle.jdbc.driver.OracleDriver");

		Connection conn = DriverManager.getConnection(
				"jdbc:oracle:thin:@shr-PC:1521:LIBRARYS", "system", "admin");
		Statement stmt = conn.createStatement();

		ResultSet rs = stmt.executeQuery("select * from T_BOOKROOM");
		while (rs.next()) {
			System.out.println(rs.getString(1));
		}

		// Connection connection = null;
		// Statement stmt = null;
		// ResultSet rs = null;
		// try {
		// Class.forName("oracle.jdbc.driver.OracleDriver");
		// connection = DriverManager.getConnection(
		// "jdbc:oracle:thin:@shr-PC:1521:lib", "system", "admin");
		// System.out.println(connection);
		// stmt = connection.createStatement();
		// rs = stmt.executeQuery("select * from user_tab_comments");
		// rs = stmt.executeQuery("select * from T_bookroom");
		//
		// while (rs.next()) {
		// System.out.println(rs.getString(1));
		// }
		//
		// } catch (Exception e) {
		// e.printStackTrace();
		// } finally {
		// try {
		// rs.close();
		// stmt.close();
		// connection.close();
		// } catch (Exception e2) {
		// }
		// }
	}

}
